use anyhow::{Context, Result};
use std::fs;
use std::fs::{File, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};

pub fn join_paths(base: &str, relative: &str) -> Result<PathBuf> {
    let base_path = Path::new(base);
    let joined = base_path.join(relative);
    Ok(joined)
}

pub fn read_file_to_string(path: &Path) -> Result<String> {
    fs::read_to_string(path).with_context(|| format!("Failed to read file at {}", path.display()))
}

pub fn write_file(file_path: &Path, content: &str, append: bool) -> io::Result<()> {
    let mut file = OpenOptions::new()
        .write(true)
        .create(true)
        .append(append)
        .truncate(!append)
        .open(file_path)?;

    file.write_all(content.as_bytes())?;
    Ok(())
}
